﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace LabLibary.ExtractData
{
    public class BC3000Info
    {
        public string Field { get; set; }
        public string Position { get; set; }
        public string Divide { get; set; }

        public BC3000Info() { }

        public BC3000Info(string _field, string _position, string _divide)
        {
            this.Field = _field;
            this.Position = _position;
            this.Divide = _divide;
        }

        public static List<BC3000Info> CreateDefault()
        {
            List<BC3000Info> _lstInfo = new List<BC3000Info>();

            BC3000Info _info = new BC3000Info("WBC", "0", "10");
            _lstInfo.Add(_info);
            _info = new BC3000Info("LYM#", "1", "10");
            _lstInfo.Add(_info);
            _info = new BC3000Info("MID#", "2", "10");
            _lstInfo.Add(_info);
            _info = new BC3000Info("GRAN#", "3", "10");
            _lstInfo.Add(_info);
            _info = new BC3000Info("LYM%", "4", "10");
            _lstInfo.Add(_info);
            _info = new BC3000Info("MID%", "5", "10");
            _lstInfo.Add(_info);
            _info = new BC3000Info("GRAN%", "6", "10");
            _lstInfo.Add(_info);
            _info = new BC3000Info("HGB", "8", "0");
            _lstInfo.Add(_info);
            _info = new BC3000Info("RBC", "7", "100");
            _lstInfo.Add(_info);
            _info = new BC3000Info("HCT", "13", "10");
            _lstInfo.Add(_info);
            _info = new BC3000Info("MCV", "10", "10");
            _lstInfo.Add(_info);
            _info = new BC3000Info("MCH", "11", "10");
            _lstInfo.Add(_info);
            _info = new BC3000Info("MCHC", "9", "0");
            _lstInfo.Add(_info);
            _info = new BC3000Info("RDW-CV", "12", "10");
            _lstInfo.Add(_info);
            _info = new BC3000Info("RDW-SD", "18", "10");
            _lstInfo.Add(_info);
            _info = new BC3000Info("PLT", "14", "0");
            _lstInfo.Add(_info);
            _info = new BC3000Info("MPV", "15", "10");
            _lstInfo.Add(_info);
            _info = new BC3000Info("PDW", "16", "10");
            _lstInfo.Add(_info);
            _info = new BC3000Info("PCT", "17", "1000");
            _lstInfo.Add(_info);

            return _lstInfo;
        }
    }



    public class BC3000
    {
        BC3000Info _bcInfo =  new BC3000Info();
        public static List<BC3000Info> _lstBc3000Info = new List<BC3000Info>();

        public BC3000()
        {
            _lstBc3000Info = BC3000Info.CreateDefault();
        }

        public static string BuildSourceString(string str)
        {
            int num = 0;
            string str2 = "";

            while (num < 4)
            {
                str2 = str2 + str.Substring(0, 4) + ",";
                str = str.Remove(0, 4).Trim();
                num++;
            }
            while (num < 9)
            {
                str2 = str2 + str.Substring(0, 3) + ",";
                str = str.Remove(0, 3).Trim();
                num++;
            }
            while (num < 12)
            {
                str2 = str2 + str.Substring(0, 4) + ",";
                str = str.Remove(0, 4).Trim();
                num++;
            }
            while (num < 14)
            {
                str2 = str2 + str.Substring(0, 3) + ",";
                str = str.Remove(0, 3).Trim();
                num++;
            }
            while (num < 15)
            {
                str2 = str2 + str.Substring(0, 4) + ",";
                str = str.Remove(0, 4).Trim();
                num++;
            }
            while (num < 0x12)
            {
                str2 = str2 + str.Substring(0, 3) + ",";
                str = str.Remove(0, 3).Trim();
                num++;
            }
            while (num < 0x13)
            {
                str2 = str2 + str.Substring(0, 4) + ",";
                str = str.Remove(0, 4).Trim();
                num++;
            }

            return str2;
        }


        public static List<ObjResults> ExtactData(string fileName)
        {
            string _SID = string.Empty;
            string _MaMayXn = "99";
            DateTime _Day = DateTime.Now;

            List<ObjResults> _lstResults = new List<ObjResults>();
            ObjResults _objResults = new ObjResults();
            string result = string.Empty;

            _lstBc3000Info = BC3000Info.CreateDefault();

            int _vitri = 0;

            try
            {
                using (StreamReader sr = new StreamReader(fileName))
                {
                    string line;
                    //while ((line = sr.ReadLine()) != null)
                    //{
                        
                    //}

                    line = sr.ReadLine();
                    line = line.Remove(0, 1).Trim();
                    while (line.Substring(0, 1) == "A")
                    {
                        line = line.Remove(0, 1);
                    }
                    line = line.Remove(0, 6);

                    _SID = Decimal.Parse(line.Substring(10, 2).ToString()).ToString();

                    string _thang = line.Substring(13, 2);
                    string _ngay = line.Substring(15, 2);
                    string _nam = line.Substring(17, 4);
                    _Day = new DateTime(int.Parse(_nam), int.Parse(_thang), int.Parse(_ngay), 0, 0, 0);

                    line = line.Remove(0, 0x19);

                    line = BuildSourceString(line);

                    string[] strArray = line.Split(new char[] { ',' });

                    foreach (BC3000Info bcInfo in _lstBc3000Info)
                    {
                        _objResults = new ObjResults();

                        _vitri = Convert.ToInt32(bcInfo.Position);
                        int _divide = Convert.ToInt32(bcInfo.Divide);

                        _objResults.SID = _SID;
                        _objResults.Ngay = _Day;
                        _objResults.May = _MaMayXn;
                        _objResults.MaMay = bcInfo.Field;

                        if (_divide != 0 && bcInfo.Divide != null)
                        {
                            _objResults.GiaTri = (decimal.Parse(strArray[_vitri]) / _divide).ToString("##0.0##");
                        }
                        else
                        {
                            _objResults.GiaTri = strArray[_vitri];
                        }

                        _lstResults.Add(_objResults);
                    }
                }

                return _lstResults;
            }
            catch
            {

            }

            return null;
        }
    }
}
